<template>
    <div class="addProject business-workAdd">
        <div class="addForm zll-form">
			<p v-if="type !== 'add'">
                <span>创建人：</span><span>{{createrName}}</span>
            </p>
            <div class="tableList" style="margin-top: 10px;">
                <table border="1">				
                    <tr>								
                        <th>使用部门</th>
                        <th>终端名称</th>
                        <th>Mac地址</th>
                        <th>访问授权</th>
                    </tr>
                    <tr class="add_Table" v-for="(item,index) in addForm" :key="index">
                        <td>
                            <el-select v-model="item.deptId" placeholder="" class="text_center" clearable :disabled="type == 'edit'">
								<el-option v-for="dept in deptList" :key="dept.id" :label="dept.deptName" :value="dept.id"></el-option>
                            </el-select>
                        </td>
                        <td><el-input v-model="item.terminalName" placeholder="" class="text_center" clearable :disabled="type == 'edit'"></el-input></td>
                        <td><el-input v-model="item.macAddress" placeholder="" class="text_center" clearable :disabled="type == 'edit'"></el-input></td>
                        <td>
                            <el-select v-model="item.accessAuthorization" placeholder="" class="text_center" clearable :disabled="type == 'see'">
                                <el-option label="启用" value="0"></el-option>
                                <el-option label="停用" value="1"></el-option>
                            </el-select>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</template>
<script>
import {getStore} from "../../../../plugins/localStore";
export default {
    props: ['editData','type','deptList'],
    data(){
        return {
            addForm:[],

			createrName: '',

			count: 0
        }
    },
    methods: {
		submitForm(val,type) {
			let self = this;
			let submitList = [];
			for (let data of self.addForm) {
				if (!data.deptId && !data.terminalName && !data.macAddress){
					continue;
				}
				submitList.push(data);
			}

			for (let data of submitList){
				if (!data.deptId){
					self.$message.warning('请选择部门');
					return -1;
				}

				if (!data.terminalName){
					self.$message.warning('请输入终端名称');
					return -1;
				}

				if (!data.macAddress){
					self.$message.warning('请输入mac地址');
					return -1;
				}

				if (!data.accessAuthorization){
					self.$message.warning('请选择访问授权');
					return -1;
				}
			}

			self.operate(submitList);

		},

		operate(submitList) {
			if (this.type == 'add'){
				this.addTerminalList(submitList);
			} else if (this.type == 'edit'){
				this.updateTerminal(submitList);
			}
		},


		addTerminalList(submitList) {
			let self = this;
			if (submitList.length == 0){
				self.$message.warning('至少添加一条记录！');
				return;
			}

			let user = getStore();
			let macAddressList = [];
			for (let data of submitList){
				if (macAddressList.includes(data.macAddress)){
					self.$message.warning('输入mac地址有重复');
					return;
				}
				data.createrId = user.id;
				macAddressList.push(data.macAddress);
			}

			if (this.count == 0){
				self.count++;
				self.$http({
					url: "/jinl/terminal/addTerminalList",
					method: "post",
					data: JSON.stringify(submitList),
					dataType: 'json',
					contentType: 'application/json',
				}).then(resp=>{
					if (resp.success) {
						self.$message.success('操作成功');
						self.$emit('refreshData');
						return;
					}

					self.$message.error(resp.message);
					self.count--;
				})
			}

		},

		updateTerminal(submitList) {
			let self = this;
			if (submitList == 0){
				self.$message.warning('至少添加一条记录！');
				return;
			}
			let user = getStore();

			submitList[0].createrId = user.id;

			if (this.count == 0){
				self.count++;
				self.$http({
					url: "/jinl/terminal/updateTerminal",
					method: "post",
					data: JSON.stringify(submitList[0]),
					dataType: 'json',
					contentType: 'application/json',
				}).then(resp=>{
					if (resp.success) {
						self.$message.success('操作成功');
						self.$emit('refreshData');
						return

					}
					self.$message.error(resp.message);
					self.count--;
				})
			}
		}

    },
    mounted(){
        
    },

	watch: {
    	editData(val) {

    		this.addForm = [];
			this.createrName = '';

    		if (this.type == 'add'){
				for (let i = 0; i < 5; i++) {
					let data = {
						id : '',
						deptId : '',
						terminalName : '',
						macAddress : '',
						accessAuthorization : '0',
						createrId : ''
					};

					this.addForm.push(data);
				}

			} else{
    			let data = Object.assign({},val);

    			this.createrName = data.createrName;
    			this.addForm.push(data);

			}
		}
	}

}
</script>
<style lang="scss" scoped>
@import "@/style/dialog.scss";
.formList {
    height: 32px;
}
</style>